package examples.game;

/**
 * @author Orlin Tomov
 *
 */
public class Match implements Comparable<Match> {

	/** number for right positions */
	private int bulls;
	/** number for different positions */
	private int cows;

	/**
	 * Check matching.
	 */
	Match(Digits digits, Digits guess) {
		bulls = 0;
		cows = 0;
		for (int i = 0; i < BullsAndCows.NUM_DIGITS; i++) {
			for (int j = 0; j < BullsAndCows.NUM_DIGITS; j++) {
				if (digits.get(i) == guess.get(j)) {
					if (i == j) {
						bulls++;
					} else {
						cows++;
					}
					break;
				}
			}
		}
	}

	public Match(int bulls, int cows) {
		this.bulls = bulls;
		this.cows = cows;
	}

	public int getBulls() {
		return bulls;
	}

	public int getCows() {
		return cows;
	}

	@Override
	public int compareTo(Match other) {
		return cows - other.cows + BullsAndCows.NUM_DIGITS * (bulls - other.bulls);
	}

	@Override
	public boolean equals(Object other) {
		if (other instanceof Match) {
			return equals((Match)other);
		}
		return false;
	}

	public boolean equals(Match other) {
		return this.compareTo(other) == 0;
	}

	@Override
	public String toString() {
		return bulls + " bulls and " + cows + " cows";
	}
}
